<template>
  <div class="cats">
    <el-row>
      <el-table
        show-summary
        :summary-method="getSummaries"
        :data="saleData"
        style="width: 100%"
        border
      >
        <el-table-column prop="goodsNum" label="商品编号" align="center"></el-table-column>
        <el-table-column prop="goodsName" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="goodsImg" label="图片" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.goodsImg" alt width="50" height="50" />
          </template>
        </el-table-column>
        <el-table-column prop="specifications" label="商品详情" align="center"></el-table-column>
        <el-table-column prop="price" label="单价" align="center"></el-table-column>
        <el-table-column prop="goodsStock.storeNum" label="库存" align="center"></el-table-column>
        <el-table-column label="选择区" align="center">
          <el-table-column prop="number" label="数量" align="center" width="200">
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.number"
                @change="handleChange(scope.row)"
                label="输入数量"
              ></el-input-number>
            </template>
          </el-table-column>
          <el-table-column prop="total" label="小计" align="center"></el-table-column>
          <el-table-column prop label="操作" align="center">
            <template slot-scope="scope">
              <span
                class="el-tag el-tag--danger el-tag--small"
                style="cursor: pointer;"
                @click="removeGoods(scope.row)"
                size="mini"
              >删除</span>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row type="flex" justify="end">
        <el-button type="primary" @click="submitOrder">提交订单</el-button>
    </el-row>
    <submitOrder :off.sync="submitOrderDialog" :orderTotalMoney="orderTotalMoney"></submitOrder>
  </div>
</template>


<script>
import { getSaleList } from "@/api/store/sale.js";
import * as util from "@/util/util.js";
export default {
  name: "cat",
  components: {
      "submitOrder": () => import("@/views/store/shopPurchase/submitOrder")
  },
  data() {
    return {
      submitOrderDialog: false,
      rowData: {},
      orderDialog: false,
      status: "",
      orderNumber: "",
      selectDate: "",
      pageSize: 15,
      currentPage: 1,
      totalElements: 0,
      totalPages: 1,
      orderTotalMoney: 0,
    };
  },
  computed: {
    storeId() {
      return JSON.parse(localStorage.getItem("user")).storeId;
    },
    saleData() {
      return this.$store.state.cat.catGoods;
    }
  },
  created() {},
  methods: {
    submitOrder(){
        this.submitOrderDialog = true;
    },
    //对每列执行该方法
    getSummaries(param) {
      console.log("param", param);
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总价";
          return;
        }
        if(index == 6 || index == 7){
            const values = data.map(item => Number(item[column.property]));
            if (!values.every(value => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                } else {
                  return prev;
                }
              }, 0);
              sums[index] += " ";
            } else {
              sums[index] = "N/A";
            }
        }else{
            sums[index] = "";
            return;
        }
      });
      console.log("sums", sums);
      this.orderTotalMoney = Number(sums[7]);
      return sums;
    },
    removeGoods(row) {
      let index = this.saleData.findIndex(e => e.goodsId == row.goodsId);
      this.$store.commit("removeGoods", index);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
      console.log(`当前页: ${val}`);
    }
  }
};
</script>